package cn.sirun.com.fca.personal.center.loader;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.tencent.cloud.iov.recycler.flow.BaseRecyclerLoader;
import com.tencent.cloud.iov.recycler.item.IBlockItem;
import com.tencent.cloud.iov.util.ResourcesUtils;

import java.util.ArrayList;
import java.util.List;

import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.uitl.PersonalInfoUtils;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.block.item.impl.PersonalOptionsItem;
import cn.sirun.com.fca.personal.center.block.item.impl.PersonalOptionsListItem;
import cn.sirun.com.fca.personal.center.block.item.impl.TitleBlockItem;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActionConst;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActivityConst;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class PersonalOptionsLoader extends BaseRecyclerLoader {
    @NonNull
    @Override
    protected Observable<List<IBlockItem>> getDataObservable() {
//        return PersonalInfoManager.getInstance().getPersonalInfoObservable()
//                .subscribeOn(Schedulers.io())
//                .flatMap(new Function<PersonalInfo, ObservableSource<List<IBlockItem>>>() {
//                    @Override
//                    public ObservableSource<List<IBlockItem>> apply(final PersonalInfo personalInfo) throws
//                            Exception {
//                        if (personalInfo == null) {
//                            return Observable.empty();
//                        }
//
//
//                    }
//                })
//                .distinctUntilChanged();
        return Observable.just(createBlockItems(PersonalInfoUtils.getPersonalInfo()));
    }

    public void refreshData() {
        loadData();
    }

    private List<IBlockItem> createBlockItems(PersonalInfo personalInfo) {
        List<IBlockItem> list = new ArrayList<>();
        List<IBlockItem> personalInfoItems = new ArrayList<>();

        //注册账号
        PersonalOptionsItem phoneItem = new PersonalOptionsItem();
        phoneItem.setmTitleId(R.string.register_telephone);
        phoneItem.setmTitle(ResourcesUtils.getString(R.string.register_telephone));
        phoneItem.setmTextValue(personalInfo.getUserPhone());
        phoneItem.setmAction(PersonalCenterActivityConst.Path.CHANGE_REGISTER_PHONE);
        personalInfoItems.add(phoneItem);

        //注册邮箱
        PersonalOptionsItem emailItem = new PersonalOptionsItem();
        emailItem.setmTitleId(R.string.register_email);
        emailItem.setmTitle(ResourcesUtils.getString(R.string.register_email));
        emailItem.setmTextValue(personalInfo.getEmail());
        emailItem.setmAction(PersonalCenterActivityConst.Path.CHANGE_REGISTER_PHONE);
        personalInfoItems.add(emailItem);

        list.add(new PersonalOptionsListItem(personalInfoItems));

        // 紧急联系人
        TitleBlockItem emergencyItem = new TitleBlockItem(
                ResourcesUtils.getString(R.string.emergency_contact));
        list.add(emergencyItem);

        PersonalOptionsItem emergencyInfoItem = new PersonalOptionsItem();
        emergencyInfoItem.setmTitleId(R.string.emergency_contact);
        StringBuilder emergencyInfo = new StringBuilder();

        // 紧急联系人名称
        if (!TextUtils.isEmpty(personalInfo.getEmergencyContactPerson())) {
            emergencyInfo.append(personalInfo.getEmergencyContactPerson());
        }
        // 紧急联系人电话
        if (!TextUtils.isEmpty(personalInfo.getEmergencyContactNumber())) {
            if (!TextUtils.isEmpty(personalInfo.getEmergencyContactPerson())) {
                emergencyInfo.append(" ");
            }
            emergencyInfo.append(personalInfo.getEmergencyContactNumber());
        }
        // 暂未填写
        if (emergencyInfo.toString().isEmpty()) {
            emergencyInfo.append(ResourcesUtils.getString(R.string.no_fill_in));
        }
        emergencyInfoItem.setmTextValue(emergencyInfo.toString());
        emergencyInfoItem.setmAction(PersonalCenterActivityConst.Path.CHANGE_EMERGENCY_CONTACT);
        list.add(emergencyInfoItem);

        // 经销商
        TitleBlockItem distributorItem = new TitleBlockItem(
                ResourcesUtils.getString(R.string.distributor));
        list.add(distributorItem);

        StringBuilder distributorInfo = new StringBuilder();

        if (personalInfo.getReseller() != null) {
            distributorInfo.append(personalInfo.getReseller().getProvince());
            distributorInfo.append(personalInfo.getReseller().getCity());
            distributorInfo.append(" / ");
            distributorInfo.append(personalInfo.getReseller().getName());
        } else {
            distributorInfo.append(ResourcesUtils.getString(R.string.no_fill_in));
        }

        PersonalOptionsItem distributorInfoItem = new PersonalOptionsItem();
        distributorInfoItem.setmTitleId(R.string.distributor);
        distributorInfoItem.setmTextValue(distributorInfo.toString());
        distributorInfoItem.setmAction(PersonalCenterActivityConst.Path.MODIFY_DISTRIBUTOR);
        list.add(distributorInfoItem);

        // 安全设置
        TitleBlockItem securityItem = new TitleBlockItem(
                ResourcesUtils.getString(R.string.security_setting));
        list.add(securityItem);

        List<IBlockItem> securitySettingsItems = new ArrayList<>();
        // 修改登录密码
        PersonalOptionsItem modifyLoginPasswordItem = new PersonalOptionsItem();
        modifyLoginPasswordItem.setmTitleId(R.string.modify_login_pass);
        modifyLoginPasswordItem.setmTitle(ResourcesUtils.getString(R.string.modify_login_pass));
        modifyLoginPasswordItem.setmAction(PersonalCenterActivityConst.Path.CONFIRM_PASSWORD);
        securitySettingsItems.add(modifyLoginPasswordItem);

        // 修改PIN
        PersonalOptionsItem modifyPinItem = new PersonalOptionsItem();
        modifyPinItem.setmTitleId(R.string.resent_pin_code);
        modifyPinItem.setmTitle(ResourcesUtils.getString(R.string.resent_pin_code));
        modifyPinItem.setmAction(PersonalCenterActivityConst.Path.MODIFY_PIN);
        securitySettingsItems.add(modifyPinItem);

        list.add(new PersonalOptionsListItem(securitySettingsItems));

        // 第三方账号关联
        TitleBlockItem openBindItem = new TitleBlockItem(
                ResourcesUtils.getString(R.string.open_bind));
        list.add(openBindItem);

        // 关联微信
        PersonalOptionsItem bindWeChatItem = new PersonalOptionsItem();
        bindWeChatItem.setmTitleId(R.string.open_bind_wechat);
        bindWeChatItem.setmSwitch(personalInfo.isWxBind());
        bindWeChatItem.setmTitle(ResourcesUtils.getString(R.string.open_bind_wechat));
        list.add(bindWeChatItem);

        // 状态
        TitleBlockItem statusItem = new TitleBlockItem(
                ResourcesUtils.getString(R.string.status));
        list.add(statusItem);

        PersonalOptionsItem logoutItem = new PersonalOptionsItem();
        logoutItem.setmTitleId(R.string.logout);
        logoutItem.setmTitle(ResourcesUtils.getString(R.string.logout));
        logoutItem.setmAction(PersonalCenterActionConst.ACTION_QUIT_APP);
        list.add(logoutItem);
        return list;
    }
}
